package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationOrderAssignReceiveDetailView;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.IView.IReceiveOrBackEngineeringOrderView;
import com.cloudsigns.factorymanager2.engineering.adapter.AddPublicationOrderAdapter;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;

import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationOrderAssignReceiveDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationOrderDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.ReceiveOrBackEngineeringOrderPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tony on 2018/5/17.
 */

public class ReceivePublicationOrderActivity extends Activity implements View.OnClickListener, IPublicationOrderDetailView, IReceiveOrBackEngineeringOrderView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_project_name, tv_engineering_name;
    private TextView edt_publication_date, edt_dispath_order_company_colon, edt_dispath_order_person_colon, edt_dispath_order_time_colon,edt_receive_people_colon,edt_reception_time_colon;
    private GridView gridView;
    private ScrollView scrollview;
    private LinearLayout ll_region;
    private RelativeLayout rl_project;
    private LinearLayout ll_engineering_info_content;
    private ImageView img_project_select,img_infor_select;
    private TextView btn_receive_order, btn_back_order;
    private final String TAG = ReceivePublicationOrderActivity.class.getSimpleName();
    private GridAdapter gridAdapter;
    private List<String> picUrlList = new ArrayList<>();
    private boolean isClick = true;
    private OrderParentBean orderParentBean;
    private PublicationOrderDetailPresenter publicationOrderDetailPresenter;
    //    private PublicationOrderAssignReceiveDetailPresenter publicationOrderAssignReceiveDetailPresenter;
    private PublicationOrderDetailBean publicationOrderDetailBean;
    Map<String, List<MediaBean>> meidaBeanMap = new HashMap<>();
    private LayoutInflater layoutInflater;
    private CommonEnum.STATUS status;
    private ReceiveOrBackEngineeringOrderPresenter receiveOrBackEngineeringOrderPresenter;
    private String module = "";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_receive_publication_order);


        publicationOrderDetailPresenter = new PublicationOrderDetailPresenter(this);
        receiveOrBackEngineeringOrderPresenter = new ReceiveOrBackEngineeringOrderPresenter(this);
        layoutInflater = getLayoutInflater();
        init();
        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
            module = mIntent.getStringExtra(ConstantUtil.KEY_2);

            if(null != module  && !TextUtils.isEmpty(module)){
                if(module.equals("publication")){
                    tv_title.setText("上刊工单");
                }else if(module.equals("")){
                    tv_title.setText("巡查工单");
                }

            }
        }

        publicationOrderDetailPresenter.getPublicationOrderDetail();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        edt_publication_date = findViewById(R.id.edt_publication_date);
        edt_dispath_order_company_colon = findViewById(R.id.edt_dispath_order_company_colon);
        edt_dispath_order_person_colon = findViewById(R.id.edt_dispath_order_person_colon);
        edt_dispath_order_time_colon = findViewById(R.id.edt_dispath_order_time_colon);
        edt_receive_people_colon=findViewById(R.id.edt_receive_people_colon);
        edt_reception_time_colon=findViewById(R.id.edt_reception_time_colon);
        rl_project = findViewById(R.id.rl_project);
        ll_engineering_info_content = findViewById(R.id.ll_engineering_info_content);
        img_project_select = findViewById(R.id.img_project_select);
        img_infor_select=findViewById(R.id.img_infor_select);
        btn_receive_order = findViewById(R.id.btn_receive_order);
        btn_back_order = findViewById(R.id.btn_back_order);
        gridView = findViewById(R.id.gridView);
        scrollview = findViewById(R.id.scrollview);
        ll_region = findViewById(R.id.ll_region);

        tv_title.setText(getResources().getString(R.string.publication));
        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        img_infor_select.setBackgroundResource(R.mipmap.arrow_down_icon);

        rl_back.setOnClickListener(this);
        btn_receive_order.setOnClickListener(this);
        btn_back_order.setOnClickListener(this);
        img_project_select.setOnClickListener(this);
        img_infor_select.setOnClickListener(this);

        gridView.setNumColumns(3);
        gridAdapter = new GridAdapter(this);
        gridAdapter.setData(picUrlList);
        gridView.setAdapter(gridAdapter);

    }

    private void initData() {
        if (null != publicationOrderDetailBean) {
            tv_project_name.setText(publicationOrderDetailBean.getPublicationContent());
            tv_engineering_name.setText(publicationOrderDetailBean.getRegionName());
            edt_publication_date.setText(DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getBeginDate()) + "/" + DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getCompleteDate()));
            edt_dispath_order_company_colon.setText(publicationOrderDetailBean.getCompanyName());
            edt_dispath_order_person_colon.setText(publicationOrderDetailBean.getCreateOrderAccountName());
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getSendOrderTime()));


            List<MediaBean> datas = publicationOrderDetailBean.getMediaList();
            if (null != datas) {
                for (MediaBean mediaRegionBean : datas) {
                    String areaName = mediaRegionBean.getAreaName();

                    if (meidaBeanMap.containsKey(areaName)) {
                        meidaBeanMap.get(areaName).add(mediaRegionBean);
                    } else {
                        List<MediaBean> beanList = new ArrayList<>();
                        beanList.add(mediaRegionBean);
                        meidaBeanMap.put(areaName, beanList);

                    }
                }

                updateRegionMeida();
            }


        }
    }

    private void updateRegionMeida() {


        if (null != meidaBeanMap && null != ll_region) {
            ll_region.removeAllViews();

            for (Map.Entry<String, List<MediaBean>> entry : meidaBeanMap.entrySet()) {
                View view = layoutInflater.inflate(R.layout.layout_point_location_content, null);
                TextView tv_area_name = view.findViewById(R.id.tv_area_name);
                tv_area_name.setText(entry.getKey());

                ListView lv_media = view.findViewById(R.id.lv_media);

                List<MediaBean> mediaRegionBeanList = entry.getValue();
                AddPublicationOrderAdapter adapter = new AddPublicationOrderAdapter(this, TAG);
                adapter.setData(mediaRegionBeanList);
                lv_media.setAdapter(adapter);

                ll_region.addView(view);


            }
        }

        scrollview.smoothScrollTo(0, 0);

    }

    // 项目名称 收放
    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    // 上刊信息 收放
    private void showEngineeringInfoSelect() {
        if (scrollview.getVisibility() == View.VISIBLE) {
            scrollview.setVisibility(View.GONE);
            img_infor_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            scrollview.setVisibility(View.VISIBLE);
            img_infor_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    private void setButton() {
        if (isClick) {
            btn_receive_order.setClickable(false);
            btn_receive_order.setBackgroundResource(R.color.rgb_00B050);
            btn_back_order.setClickable(false);
            btn_back_order.setBackgroundResource(R.color.rgb_C00000);
        } else {
            btn_receive_order.setClickable(false);
            btn_receive_order.setBackgroundResource(R.color.rgb_ffffffff);
            btn_back_order.setClickable(false);
            btn_back_order.setBackgroundResource(R.color.rgb_ffffffff);
        }
    }

    private void receiveOrder() {
        status = CommonEnum.STATUS.RECEIVED;
        isClick = false;
        setButton();
        receiveOrBackEngineeringOrderPresenter.receiveOrbackEngineeringOrder();
    }

    private void backOrder() {
        status = CommonEnum.STATUS.RETURNED;
        isClick = false;
        setButton();
        receiveOrBackEngineeringOrderPresenter.receiveOrbackEngineeringOrder();


    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
                // 接收工单
            case R.id.btn_receive_order:
                if (isClick)
                    receiveOrder();
                break;
                // 退回工单
            case R.id.btn_back_order:
                if (isClick)
                    backOrder();
                break;
                // 项目名称 收放
            case R.id.img_project_select:
                showEngineeringInfoDialog();
                break;
            // 上刊信息
            case R.id.img_infor_select:
                showEngineeringInfoSelect();
                break;

        }
    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return "";
    }

    @Override
    public void getPublicationOrderDetailSuccessed(PublicationOrderDetailBean publicationOrderBean) {
        this.publicationOrderDetailBean = publicationOrderBean;
        initData();
    }

    @Override
    public void gePublicationOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }
//
//    @Override
//    public void getPublicationOrderAssignReceiveDetailSuccessed(PublicationOrderDetailBean publicationOrderBean) {
//        this.publicationOrderDetailBean = publicationOrderBean;
//        initData();
//    }
//
//    @Override
//    public void getPublicationOrderAssignReceiveDetailFailed(String desc) {
//        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
//    }

    @Override
    public CommonEnum.EngineeringORderCategory getEngineeringDeptOrderCategoryEnum() {
        if (null != orderParentBean)
            return orderParentBean.getCategory();
        return null;
    }

    @Override
    public String getId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return "";
    }

    @Override
    public CommonEnum.STATUS getStatus() {
        return status;
    }

    @Override
    public void receiveOrBackOrderSuccessed() {
        if (null != status) {
            if (status == CommonEnum.STATUS.RECEIVED)
                Toast.makeText(this, "接收成功！", Toast.LENGTH_SHORT).show();
            else if (status == CommonEnum.STATUS.RETURNED)
                Toast.makeText(this, "退回成功！", Toast.LENGTH_SHORT).show();
        }
        Intent mIntent = new Intent();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void receiveOrBackOrderFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
        isClick = true;
        setButton();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
